<?php

	// Session
	session_start();

	// Kjo skede ben numerimin e vizitueshmeris se librarsi
	// Ne varesi nga opcini i kerkuar, scripti ben edhe numerim te detalizhuar
	// per kohen, vizitorin dhe te dhenat e tjera
	
	
	// Bashkangjitja e skedes me funksione
	include('../include/functions.inc.php');
	
	
	// DB CONNECT
	dbOpen("comunity");

	
	if (isset($_GET['id'])) {
		
		// Cakto vleren para aktualizimit
		$book = mysql_fetch_assoc(mysql_query("SELECT access,counter FROM library WHERE id = '".$_GET['id']."'"));
		
		// Nese libri eshte vetem per vizitoret e registruar
		if ($book['access'] == '2' && !isset($_SESSION['user_id'])) {
			
			// Kalo ne faqen intro
			header("Location: ../message.php?sid=book&id=locked");
			exit;
		}
		else {
			
			$counter1 = $book['counter'];
			$counter2 = implode(mysql_fetch_assoc(mysql_query("SELECT counter FROM contents WHERE book_id = '".$_GET['id']."'")));
			
			// Aktualizo vlerat e numruesve
			$counter1++;
			$counter2 = $counter2.",".$_SERVER['REQUEST_TIME'];
			 
			// Arkivo vleren e re ne tabelen e librave
			mysql_query("UPDATE library SET counter = '$counter1' WHERE id='".$_GET['id']."'");
			mysql_query("UPDATE contents SET counter = '$counter2' WHERE book_id='".$_GET['id']."'"); 
			
			// Kalo ne faqen intro
			header("Location: preface.php?id=".$_GET['id']."");
			exit;
		}
	}
	// Nese faqa hapet nga burim extern, trego porosin per gabim
	else
		header("Location: ../message.php?sid=system&id=error");
		
	
	
?>